(function($, doc) {
	$.init({});		
	var userinfo = app.getState() || {};
    var uid = userinfo.user_id;
    var address_id=0;
    var goods_id=0;
    var shop_price=0;
    var sale_id=0;
    var shipping_price=0;
    
    window.onload = function(){
    	mui.plusReady(function(){
	        goods_id=plus.webview.currentWebview().goods_id;
	        var width=document.documentElement.clientWidth;
	    	var height=document.documentElement.clientHeight;
	    	document.getElementById('account_pwd').style.width=width+'px';
	    	document.getElementById('account_pwd').style.height=height+'px';
    	
	    	//从服务器获取数据
		  	$.ajax({
					url: apiurl + "Store/show_goods/",
					data: {'goods_id':goods_id,'user_id':uid,'_token':userinfo.token},
					async: true,
					dataType: 'json',
					crossDomain: true, //强制使用5+跨域
					type: 'post',
					timeout: 10000,
					success: function(data) {
						console.log(JSON.stringify(data));
						if(data.status ==-3){
							mui.toast(data.msg);
							app.out();
							return false;
						}
						// 请求成功
						if(data.user_address && data.user_address.address_id>0){
							address_id=data.user_address.address_id;
							var htm='';
							htm+='<p>'+data.user_address.addrdesc+data.user_address.address+' '+data.user_address.username+' '+data.user_address.mobile;
							htm+='<i class="mui-icon mui-icon-arrowright"></i></p>';
							document.querySelector('.shop-order-address').innerHTML=htm;
						}
						else{
							address_id=0;
							var htm='';
							htm+='<p class="selecte_address"><span class="mui-icon mui-icon-plusempty"></span>';
							htm+=' 请先添加收货地址 <i class="mui-icon mui-icon-arrowright"></i></p>';
							document.querySelector('.shop-order-address').innerHTML=htm;
						}
						document.getElementById('goods_name').innerHTML=data.good.goods_name;
						document.getElementById('shop_price').innerHTML=data.good.shop_price;
						shop_price=data.good.shop_price*1;
						document.getElementById('shipping_price').innerHTML=data.good.shipping_price;
						shipping_price=data.good.shipping_price*1;
						
						document.getElementById('all_money').innerHTML=shop_price+shipping_price;
						if(data.good.original_img.indexOf(upurl+'Uploads') !== -1){
							document.getElementById('original_img').src=data.good.original_img;
						}
						else{
							document.getElementById('original_img').src=upurl+data.good.original_img;
						}
						//document.getElementById('original_img').src=serverurl+data.good.original_img;
						document.getElementById('buyer_name').innerHTML='向 ( '+data.buyer.nickname+' ) 支付';
						sale_id=data.store.user_id;
					},
					error: function(xhr, type, errorThrown) {
						//console.log(JSON.stringify(xhr));
						//console.log(JSON.stringify(type));
						//console.log(JSON.stringify(errorThrown));
						mui.toast('获取数据失败！');
					}
			});
		  	
			//关闭等待框
			plus.nativeUI.closeWaiting();
			//显示当前页面
			mui.currentWebview.show();
		})
    }
    
    mui(".shop-order-address").on('tap','p',function(){
    	if(address_id==0){
    		//打开新建地址页
			mui.openWindow({
				id: 'add_address.html',
				url: '/pages/setting/add_address.html',
				extras: {
					addr_id: 0 //自定义扩展参数，可以用来处理页面间传值
				},
				show: {
					autoShow: false
				},
				waiting: {
					autoShow: true,
					title: '正在加载...'
				}
			});
    	}
		else{
			//打开选择地址页
			mui.openWindow({
				id: 'select_address.html',
				url: '/pages/setting/select_address.html',
				show: {
					autoShow: false
				},
				waiting: {
					autoShow: true,
					title: '正在加载...'
				}
			});
		}
	}) 
    
    
    window.addEventListener('refresh_address',function(event){
    	address_id = event.detail.address_id;
    	var username=event.detail.username;
    	var addrdesc=event.detail.addrdesc;
    	var address=event.detail.address;
    	var mobile=event.detail.mobile;
    	var htm='';
		htm+='<p>'+addrdesc+address+' '+username+' '+mobile;
		htm+='<i class="mui-icon mui-icon-arrowright"></i></p>';
		document.querySelector('.shop-order-address').innerHTML=htm;
	});
	
	
	doc.getElementById('plus_num').addEventListener('tap', function() {
		var old_num=parseInt(document.getElementById('goods_num').value);
		var price=document.getElementById('shop_price').innerHTML.trim();
		price=price*1;
		if(old_num==1){
			return true;
		}
		old_num=old_num-1;
		document.getElementById('goods_num').value=old_num;
		var all_money=old_num*price*1+shipping_price*1;
		all_money=toDecimal2(all_money);
		document.getElementById('all_money').innerHTML= all_money;
	})
	
	doc.getElementById('add_num').addEventListener('tap', function() {
		var old_num=parseInt(document.getElementById('goods_num').value);
		var price=document.getElementById('shop_price').innerHTML.trim();
		price=price*1;
		
		old_num=old_num+1;
		document.getElementById('goods_num').value=old_num;
		var all_money=old_num*price*1+shipping_price*1;
		all_money=toDecimal2(all_money);
		document.getElementById('all_money').innerHTML=all_money;
	})
	
	
	//制保留2位小数，如：2，会在2后面补上00.即2.00 
	function toDecimal2(x) {
		var f = parseFloat(x);
		if(isNaN(f)) {
			return '';
		}
		var f = Math.floor(x * 100) / 100;
		var s = f.toString();
		var rs = s.indexOf('.');
		if(rs < 0) {
			rs = s.length;
			s += '.';
		}
		while(s.length <= rs + 2) {
			s += '0';
		}
		return s;
	}
	
	
	doc.getElementById('do_buy').addEventListener('tap', function() {
		var all_money=document.getElementById('all_money').innerHTML.trim();
		if(parseFloat(userinfo.a1_money)<parseFloat(all_money)){
			mui.alert('余额不足');
			return false;
		}
		if(address_id==0){
			mui.alert('没有收货地址');
			return false;
		}
		if(sale_id==userinfo.user_id){
			mui.alert('用户不能购买自己店铺的商品');
			return false;
		}
		document.getElementById('remark').blur();
		document.getElementById('account_pwd').style.display="block";
		document.getElementById('main_content').style.display="block";
		document.getElementById('keyboard').style.display="block";
		document.getElementById('final_integer').innerHTML=all_money;
		var pwd_array=mui('.mui-input-row.mui-password input[type="password"]');
			mui.each(pwd_array,function(index,item){
				if(item.value!=''){
				   item.value='';
				}
			}) ;
	})
	
	//点击数字
	mui('#keyboard').on('tap', '.num.data_num', function() {
		var number = this.getAttribute('data-num');
		var pwd_array = mui('.mui-input-row.mui-password input[type="password"]');
		var f = false;
		var pwd = '';
		mui.each(pwd_array, function(index, item) {
			if(item.value == '') {
				item.value = number;
				return false;
			}
		});
	
		mui.each(pwd_array, function(index, item) {
			if(item.value == '') {
				f = true;
				return false;
			}
			pwd += item.value;
		});
	
		if(!f) {
			var num = doc.getElementById('goods_num').value;
			num = num * 1;
			var Info = {
				user_id: userinfo.user_id,
				trade_pwd: pwd,
				goods_id: goods_id,
				address_id: address_id,
				num: num,
				remark: document.getElementById('remark').value.trim(),
				_token:userinfo.token
			};
			document.getElementById('account_pwd').style.display = "none";
			document.getElementById('main_content').style.display = "none";
			document.getElementById('keyboard').style.display = "none";
			document.getElementById('final_integer').innerHTML = '';
			mui.ajax({
				url: apiurl + "Store/doBuy/",
				data: Info,
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'post',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					if(res.status ==-3){
						mui.toast(res.msg);
						app.out();
						return false;
					}
					// 请求成功
					if(res.status ==0) {
						mui.alert(res.msg);
					} else {
						plus.nativeUI.toast(res.msg);
						
						userinfo.a1_money = res.data.a1_money;
						userinfo.a2_money = res.data.a2_money;
						app.setState(userinfo);
						mui.fire(plus.webview.getWebviewById('wallet.html'), 'refresh_data');
						mui.back();
					}
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('操作失败！');
				}
			});
		}
	
	})
			
	document.getElementById('close_win').addEventListener('tap', function() {
		document.getElementById('account_pwd').style.display="none";
		document.getElementById('main_content').style.display="none";
		document.getElementById('keyboard').style.display="none";
		document.getElementById('final_integer').innerHTML='';
	})
	
	//点击删除
	mui('#keyboard').on('tap', '#numC', function() {
		var pwd_array = mui('.mui-input-row.mui-password input[type="password"]');
		var len = pwd_array.length;
		for(i = len - 1; i >= 0; i--) {
			if(pwd_array[i].value != '') {
				pwd_array[i].value = '';
				break;
			}
		}
	});
			

}(mui, document));
    